#include "worker.h"
#include "u.h"
#include <QDebug>
#include <QRandomGenerator>
#include <QThread>
#include "global.h"
#include "logmanager.h"

Worker::Worker(QObject *parent)
    : QThread(parent), m_logPeriodMillionSecond(3600), m_collection(true), m_weightUnitDecimalPlaces(3)
{}

bool Worker::init(QString& err)
{
    do{
        m_pSerialTemperature = new SerialPortSync();
        if (!m_pSerialTemperature->open(Global::instance().getStr("Serial-A"))){
            err= "Can not open serialPort Temperature!";
            break;
        }

        // m_pSerialWeight = new SerialPortSync();
        // if (!m_pSerialWeight->open(Global::instance().getStr("Serial-D"))){
        //     err= "Can not open serialPort Weight!";
        //     break;
        // }

        m_pSerialPh = new SerialPortSync();
        if (!m_pSerialPh->open(Global::instance().getStr("Serial-C"), QSerialPort::Baud19200)){
            err= "Can not open serialPort Speed!";
            break;
        }

        m_pSerialConductivity = new SerialPortSync();
        if (!m_pSerialConductivity->open(Global::instance().getStr("Serial-D"), QSerialPort::Baud19200)){
            err= "Can not open serialPort Ph!";
            break;
        }
        m_pSerialSpeed = new SerialPortSync();
        if (!m_pSerialSpeed->open(Global::instance().getStr("Serial-B"))){
            err= "Can not open serial Speed!";
            break;
        }
    }while(0);
    if(err.length() > 0){
        qDebug()<< "Open serialPort failure->" <<err;
        return false;
    }
    return true;
}

float Worker::setRound(float value)
{
    float tmp = 1.0;
    for (int i=0; i<m_weightUnitDecimalPlaces; i++){
        tmp = 10*tmp;
    }
    return std::round(value * tmp) / tmp;
}

float Worker::getTemperature()
{
    // const char rawData[] = {0x02, 0x04, 0x00, 0x00, 0x00, 0x01, 0x31, (char)0xF9};
    // QByteArray sendData = QByteArray(rawData, sizeof(rawData));

    QByteArray sendData = QByteArray::fromHex("02 04 00 00 00 01 31 F9");
    // qDebug() << U::getThDt()<< "temperature sendData<- " << sendData.toHex();
    QByteArray response = m_pSerialTemperature->sendSync(sendData,200); // 5秒超时
    if (!response.isEmpty() && response.size()>6) {
        unsigned char bytes[2];
        bytes[0] = static_cast<unsigned char>(response[4]);  // 最低位字节
        bytes[1] = static_cast<unsigned char>(response[3]);
        short  temp =0;
        std::memcpy(&temp, bytes, sizeof(short));
        LogManager::ins().log(QtDebugMsg, QString("temperature rev<-%1<-result:%2").arg(response.toHex()).arg(temp*0.1));
        return temp*0.1;
    } else {
        qDebug() << "temperature未收到响应或超时";
        return -300.0;
    }
}

float Worker::getWeight()
{
//    const char rawData[] = {0x0A, 0x03, 0x00, 0x00, 0x00, 0x02, (char)0xC5, (char)0x70};
    QByteArray sendData = QByteArray::fromHex("0A 03 00 00 00 02 C5 70");
    // qDebug() << U::getThDt()<< "Weight send<- " << sendData.toHex();
    QByteArray response = m_pSerialTemperature->sendSync(sendData,500); // 200毫秒超时

    if (!response.isEmpty() && response.size()>8) {
        unsigned char bytes[4];
        bytes[0] = static_cast<unsigned char>(response[4]);  // 最低位字节
        bytes[1] = static_cast<unsigned char>(response[3]);
        bytes[2] = static_cast<unsigned char>(response[6]);
        bytes[3] = static_cast<unsigned char>(response[5]);
        float  retureValue =0;
        std::memcpy(&retureValue, bytes, sizeof(float));
        LogManager::ins().log(QtDebugMsg, QString("weight rev<-%1<-result:%2").arg(response.toHex()).arg(retureValue));
        return retureValue;
    } else {
        qDebug() << "weight未收到响应或超时";
        return -300.0;
    }
}

float Worker::getPh()
{
    QByteArray sendData = QByteArray::fromHex("01 03 08 29 00 0A 16 65");
    // qDebug() << U::getThDt()<< "ph send->" << sendData.toHex();
    QByteArray response = m_pSerialPh->sendSync(sendData,200); // 5秒超时

    if (response.size()>24) {
        uint8_t bytes[4];
        bytes[0] = static_cast<unsigned char>(response[8]);  // 最低位字节
        bytes[1] = static_cast<unsigned char>(response[7]);
        bytes[2] = static_cast<unsigned char>(response[10]);
        bytes[3] = static_cast<unsigned char>(response[9]);
        float  temp =0;
        std::memcpy(&temp, bytes, sizeof(float));
        LogManager::ins().log(QtDebugMsg, QString("ph recv<-%1 %2").arg(response.toHex()).arg(temp));
        return temp;
    } else {
        qDebug() << "Ph未收到响应或超时";
        return -300.0;
    }
}

float Worker::getPhTemperature()
{
    QByteArray sendData = QByteArray::fromHex("01 03 09 69 00 0A 16 4D");   // ph的实时温度
    // qDebug() << U::getThDt()<< "ph send->" << sendData.toHex();
    // 返回值：01 03 14 00 04 00 00 A5 90 41 E7 00 00 00 00 00 00 C1 A0 00 00 43 02 6D 4E
    QByteArray response = m_pSerialPh->sendSync(sendData,200); // 5秒超时

    if (response.size()>24) {
        uint8_t bytes[4];
        bytes[0] = static_cast<unsigned char>(response[8]);  // 最低位字节
        bytes[1] = static_cast<unsigned char>(response[7]);
        bytes[2] = static_cast<unsigned char>(response[10]);
        bytes[3] = static_cast<unsigned char>(response[9]);
        float  temp =0;
        std::memcpy(&temp, bytes, sizeof(float));
        LogManager::ins().log(QtDebugMsg, QString("phTemperature recv<-%1 %2").arg(response.toHex()).arg(temp));
        return temp;
    } else {
        qDebug() << "Ph温度未收到响应或超时";
        return -300.0;
    }
}

float Worker::getPhUsageTime()
{
    QByteArray sendData = QByteArray::fromHex("01 03 12 43 00 06 31 64");   // ph的实时温度
    // qDebug() << U::getThDt()<< "ph send->" << sendData.toHex();
    // 返回值：01 03 0C 25 DA 41 03 00 00 00 00 00 00 00 00 D0 39
    QByteArray response = m_pSerialPh->sendSync(sendData,200); // 5秒超时

    if (response.size() == 17) {
        uint8_t bytes[4];
        bytes[0] = static_cast<unsigned char>(response[4]);  // 最低位字节
        bytes[1] = static_cast<unsigned char>(response[3]);
        bytes[2] = static_cast<unsigned char>(response[6]);
        bytes[3] = static_cast<unsigned char>(response[5]);
        float  temp =0;
        std::memcpy(&temp, bytes, sizeof(float));
        LogManager::ins().log(QtDebugMsg, QString("phUsageTime recv<-%1 %2").arg(response.toHex()).arg(temp));
        return temp;
    } else {
        qDebug() << "Ph温度未收到响应或超时";
        return -300.0;
    }
}


float Worker::getConductivityTemperature()
{
    QByteArray sendData = QByteArray::fromHex("01 03 09 69 00 0A 16 4D");   // ph的实时温度
    // qDebug() << U::getThDt()<< "ph send->" << sendData.toHex();
    // 返回值：01 03 14 00 04 00 00 A5 90 41 E7 00 00 00 00 00 00 C1 A0 00 00 43 02 6D 4E
    QByteArray response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时

    if (response.size()>24) {
        uint8_t bytes[4];
        bytes[0] = static_cast<unsigned char>(response[8]);  // 最低位字节
        bytes[1] = static_cast<unsigned char>(response[7]);
        bytes[2] = static_cast<unsigned char>(response[10]);
        bytes[3] = static_cast<unsigned char>(response[9]);
        float  temp =0;
        std::memcpy(&temp, bytes, sizeof(float));
        LogManager::ins().log(QtDebugMsg, QString("conductivityTemperature recv<-%1 %2").arg(response.toHex()).arg(temp));
        return temp;
    } else {
        qDebug() << "Ph温度未收到响应或超时";
        return -300.0;
    }
}

float Worker::getConductivityUsageTime()
{
    QByteArray sendData = QByteArray::fromHex("01 03 12 43 00 06 31 64");   // ph的实时温度
    // qDebug() << U::getThDt()<< "ph send->" << sendData.toHex();
    // 返回值：01 03 0C 25 DA 41 03 00 00 00 00 00 00 00 00 D0 39
    QByteArray response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时

    if (response.size() == 17) {
        uint8_t bytes[4];
        bytes[0] = static_cast<unsigned char>(response[4]);  // 最低位字节
        bytes[1] = static_cast<unsigned char>(response[3]);
        bytes[2] = static_cast<unsigned char>(response[6]);
        bytes[3] = static_cast<unsigned char>(response[5]);
        float  temp =0;
        std::memcpy(&temp, bytes, sizeof(float));
        LogManager::ins().log(QtDebugMsg, QString("conductivityUsageTime recv<-%1 %2").arg(response.toHex()).arg(temp));
        return temp;
    } else {
        qDebug() << "Ph温度未收到响应或超时";
        return -300.0;
    }
}


float Worker::getConductivity()
{
    QByteArray sendData = QByteArray::fromHex("01 03 08 29 00 0A 16 65");
    // qDebug() << U::getThDt()<< "Conductivity sendData<- " << sendData.toHex();
    QByteArray response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时

    if (!response.isEmpty() && response.size()>24) {
        unsigned char bytes[4];
        bytes[0] = static_cast<unsigned char>(response[8]);  // 最低位字节
        bytes[1] = static_cast<unsigned char>(response[7]);
        bytes[2] = static_cast<unsigned char>(response[10]);
        bytes[3] = static_cast<unsigned char>(response[9]);
        float  temp =0;
        std::memcpy(&temp, bytes, sizeof(float));
        LogManager::ins().log(QtDebugMsg, QString("conductivity recv<-%1 %2").arg(response.toHex()).arg(temp));
        return temp;
    } else {
        qDebug() << "Conductivity未收到响应或超时";
        return -300.0;
    }
}

float Worker::getSpeed()
{
    QByteArray sendData = QByteArray::fromHex("01 03 00 12 00 01 24 0F");
    // qDebug() << U::getThDt()<< "speed send->" << sendData.toHex('/');
    QByteArray response = m_pSerialSpeed->sendSync(sendData,200); // 5秒超时

    if (!response.isEmpty() && response.size()==7) {
        uint8_t bytes[2];
        bytes[0] = static_cast<uint8_t>(response[4]);  // 最低位字节
        bytes[1] = static_cast<uint8_t>(response[3]);
        short  temp =0;
        std::memcpy(&temp, bytes, sizeof(short));
        LogManager::ins().log(QtDebugMsg, QString("speed rev<-%1<-result:%2").arg(response.toHex()).arg(((float)temp)/10));
        return ((float)temp)/10;
    } else {
        qDebug() << "Speed have not receive response";
        return -300.0;
    }
}

void Worker::addCmd(Command cmd)
{
    QMutexLocker locker(&m_mutex);
    m_vector.append(cmd);
}

Command Worker::getCmd()
{
    QMutexLocker locker(&m_mutex);
    if (m_vector.size()>0)
        return m_vector.takeFirst();
    else
        return Command();
}

void Worker::sendCmd(Command cmd)
{
    if (cmd.m_cmd == Global::Cmd::WeightCalibration_0){
        char rawData[] = {0x0A, 0x06, 0x00, 0x06, 0x00, 0x00, 0x68, (char)0xB0};
        if (!sendData(*m_pSerialTemperature, rawData)){
            emit signalInfo(Global::Cmd::WeightCalibration_0, Global::Result::Failure, "send Cmd Calibration_0 fail");
        }
        // QThread::msleep(100);
        char rawData1[] = {0x0A, 0x06, 0x00, 0x06, 0x00, 0x01, (char)0xA9, (char)0x70};
        if (!sendData(*m_pSerialTemperature, rawData1)){
            emit signalInfo(Global::Cmd::WeightCalibration_0, Global::Result::Failure, "send Cmd Calibration_00 fail");
        }
    }

    if (cmd.m_cmd == Global::Cmd::WeightCalibration_weight){
        char rawData[] = {0x0A, 0x06, 0x00, 0x06, 0x00, 0x00, 0x68, (char)0xB0};
        if (!sendData(*m_pSerialTemperature, rawData)){
            emit signalInfo(Global::Cmd::WeightCalibration_weight, Global::Result::Failure, "send Cmd Calibration_weight fail");
        }
        // QThread::msleep(100);
        char rawData1[]         = {0x0A, 0x06, 0x00, 0x09, 0x00, 0x01, (char)0x99, (char)0x73};
        QVector<uint8_t> data   = {0x0A, 0x06, 0x00, 0x09, 0x00, 0x01};
        data[4] = (cmd.m_data >> 8) & 0xFF;
        data[5] = cmd.m_data  & 0xFF;

        uint16_t crc = U::getCRC(data);

        rawData1[4] = data[4];
        rawData1[5] = data[5];
        rawData1[6] = (crc >> 8) & 0xFF;
        rawData1[7] = crc & 0xFF;

        if (!sendData(*m_pSerialTemperature, rawData1)){
            emit signalInfo(Global::Cmd::WeightCalibration_weight, Global::Result::Failure, "send Cmd Calibration_weight1 fail");
        }
        // QThread::msleep(100);
        char rawData2[] = {0x0A, 0x06, 0x00, 0x06, 0x00, 0x03, (char)0x28, (char)0xB1};
        if (!sendData(*m_pSerialTemperature, rawData2)){
            emit signalInfo(Global::Cmd::WeightCalibration_weight, Global::Result::Failure, "send Cmd Calibration_weight2 fail");
        }
    }

    if (cmd.m_cmd == Global::Cmd::WeightToZero){  // 归零
        char rawData[] = {0x0A, 0x06, 0x00, 0x05, 0x08, 0x00, (char)0x9F, (char)0x70};
        QByteArray sendData = QByteArray(rawData, sizeof(rawData));
        qDebug() << "send_data:" << sendData.toHex();
        QByteArray response = m_pSerialTemperature->sendSync(sendData,200); // 5秒超时
        qDebug() << "reve_data:" << response.toHex();

        char rawResult[] = {0x0A, 0x06, 0x00, 0x05, 0x00, 0x01, (char)0x59, (char)0x70};
        QByteArray result = QByteArray(rawResult, sizeof(rawResult));
        if (response != result){
            emit signalInfo(Global::Cmd::WeightToZero, Global::Result::Failure, "send Cmd ToZero fail");
        }
    }

    if (cmd.m_cmd == Global::Cmd::WeightToTare){  // 去皮
        uint8_t rawData[] = {0x0A, 0x06, 0x00, 0x05, 0x10, 0x00, 0x95, 0x70};
        QByteArray sendData = QByteArray((char*)rawData, sizeof(rawData));
        qDebug() << "send_data:" << sendData.toHex();
        QByteArray response = m_pSerialTemperature->sendSync(sendData,200); // 5秒超时
        qDebug() << "reve_data:" << response.toHex();

        char rawResult[] = {0x0A, 0x06, 0x00, 0x05, 0x00, 0x01, (char)0x59, (char)0x70};
        QByteArray result = QByteArray(rawResult, sizeof(rawResult));
        if (response != result){
            emit signalInfo(Global::Cmd::WeightToTare, Global::Result::Failure, "send Cmd Tare fail");
        }
    }

    if(cmd.m_cmd == Global::Cmd::SpeedSet){ // 设置速度
        uint8_t rawData1[]      = {0x01, 0x10, 0x01, 0x12, 0x00, 0x02, 0x04,0x04, 0xB0,0x00,0x00,0x7E,0x3D};   // 04B0是速度的10倍即1200
        QVector<uint8_t> data   = {0x01, 0x10, 0x01, 0x12, 0x00, 0x02, 0x04,0x04, 0xB0,0x00,0x00};
        data[7] = (cmd.m_data*10 >> 8) & 0xFF;
        data[8] = cmd.m_data*10  & 0xFF;

        uint16_t crc = U::getCRC(data);

        rawData1[7] = data[7];  // 设置速度高位
        rawData1[8] = data[8];  // 设置速度低位
        rawData1[11] =  (crc >> 8) & 0xFF;  // 设置CRC高位
        rawData1[12] = crc & 0xFF;          // 设置CRC低位

        QByteArray sendData = QByteArray((char*)rawData1, sizeof(rawData1));
        qDebug() << "send_data:" << sendData.toHex('/');
        QByteArray response = m_pSerialSpeed->sendSync(sendData,200); // 5秒超时
        qDebug() << "reve_data:" << response.toHex('/');

        QByteArray result = QByteArray::fromHex("01 10 01 12 00 02 e0 31");
        if (response != result){
            emit signalInfo(Global::Cmd::SpeedSet, Global::Result::Failure, "send Cmd SetSpeed fail");
        }
    }

    if(cmd.m_cmd == Global::Cmd::PhCalibration_1){ // 校准PH1
        QByteArray sendData = QByteArray::fromHex("01 10 10 BF 00 04 08 00 30 00 00 79 CE 00 F4 97 E7");    //登录S级4288
        qDebug() << U::getThDt()<< "PhCalibration_1 1登录S级 send->" << sendData.toHex('/');
        QByteArray response = m_pSerialPh->sendSync(sendData,200); // 5秒超时
        qDebug() << "PhCalibration_1 receive:" << response.toHex('/');
        QByteArray result = QByteArray::fromHex("01 10 10 BF 00 04 F4 EE");
        if (response != result){
            emit signalInfo(Global::Cmd::PhCalibration_1, Global::Result::Failure, "send PhCalibration_1 1登录S级 fail");
            return;
        }

        sendData = QByteArray::fromHex("01 10 25 01 00 02 04 00 01 00 00 C5 32");    //校准体系 9474
        qDebug() << U::getThDt()<< "PhCalibration_1 2校准体系 send->" << sendData.toHex('/');
        response = m_pSerialPh->sendSync(sendData,200); // 5秒超时
        qDebug() << "PhCalibration_1 receive:" << response.toHex('/');
        result = QByteArray::fromHex("01 10 25 01 00 02 1B 04");
        if (response != result){
            emit signalInfo(Global::Cmd::PhCalibration_1, Global::Result::Failure, "send PhCalibration_1 2校准体系 fail");
            return;
        }

        sendData = QByteArray::fromHex("01 10 14 29 00 02 04 51 EC 40 80 2F B4");    //2点校准 CP1 5162
        qDebug() << U::getThDt()<< "PhCalibration_1 2点校准 CP1 5162 send->" << sendData.toHex('/');
        response = m_pSerialPh->sendSync(sendData,200); // 5秒超时
        qDebug() << "PhCalibration_1 receive:" << response.toHex('/');
        result = QByteArray::fromHex("01 10 14 29 00 02 95 F0");
        if (response != result){
            emit signalInfo(Global::Cmd::PhCalibration_1, Global::Result::Failure, "send PhCalibration_1 2点校准 CP1 5162 fail");
            return;
        }

        // sendData = QByteArray::fromHex("01 03 15 8F 00 08 71 EB");    //2点校准 5520
        // qDebug() << U::getThDt()<< "PhCalibration_1 2点校准5520 send->" << sendData.toHex('/');
        // response = m_pSerialPh->sendSync(sendData,200); // 5秒超时
        // qDebug() << "PhCalibration_1 receive:" << response.toHex('/');
        // result = QByteArray::fromHex("01 03 10 1D 58 40 80 F9 70 43 29 AB 6F 43 94 00 00 00 00 D4 0E");
        // if (response != result){
        //     emit signalInfo(Global::Result::Failure, Global::Cmd::PhCalibration_1, "send PhCalibration_1 2点校准5520 fail");
        //     return;
        // }
        emit signalInfo(Global::Cmd::PhCalibration_1, Global::Result::Success, "");
    }
    if(cmd.m_cmd == Global::Cmd::PhCalibration_2){ // 校准PH2
        QByteArray sendData = QByteArray::fromHex("01 10 14 49 00 02 04 00 00 40 E0 F8 BD");    //CP2 5194
        qDebug() << U::getThDt()<< "PhCalibration_1 CP2 5194 send->" << sendData.toHex('/');
        QByteArray response = m_pSerialPh->sendSync(sendData,200); // 5秒超时
        qDebug() << "PhCalibration_1 receive:" << response.toHex('/');
        QByteArray result = QByteArray::fromHex("01 10 14 49 00 02 95 EE");
        if (response != result){
            emit signalInfo(Global::Cmd::PhCalibration_2, Global::Result::Failure, "send PhCalibration_1 CP2 5194 fail");
            return;
        }

        // sendData = QByteArray::fromHex("01 03 15 97 00 08 F1 EC");    //CP2 5528
        // qDebug() << U::getThDt()<< "PhCalibration_1 CP2 5528 send->" << sendData.toHex('/');
        // response = m_pSerialPh->sendSync(sendData,200); // 5秒超时
        // qDebug() << "PhCalibration_1 receive:" << response.toHex('/');
        // result = QByteArray::fromHex("01 03 10 2A 7A 40 E0 63 18 C0 E8 98 C8 43 94 00 00 00 00 91 D0 ");
        // if (response != result){
        //     emit signalInfo(Global::Result::Failure, Global::Cmd::PhCalibration_2, "send PhCalibration_1 CP2 5528 fail");
        //     return;
        // }
        emit signalInfo(Global::Cmd::PhCalibration_2, Global::Result::Success, "");
    }
    if(cmd.m_cmd == Global::Cmd::ConductivityCalibration_1){ // 校准PH1
        QByteArray sendData = QByteArray::fromHex("01 10 10 BF 00 04 08 00 30 00 00 79 CE 00 F4 97 E7");    //登录S级4288
        qDebug() << U::getThDt()<< "ConductivityCalibration_1 1登录S级 send->" << sendData.toHex('/');
        QByteArray response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时
        qDebug() << "ConductivityCalibration_1 receive:" << response.toHex('/');
        QByteArray result = QByteArray::fromHex("01 10 10 BF 00 04 F4 EE");
        if (response != result){
            emit signalInfo(Global::Cmd::ConductivityCalibration_1, Global::Result::Failure, "send ConductivityCalibration_1 1登录S级 fail");
            return;
        }

        sendData = QByteArray::fromHex("01 10 25 01 00 02 04 00 01 00 00 C5 32");    //校准体系 9474
        qDebug() << U::getThDt()<< "ConductivityCalibration_1 2校准体系 send->" << sendData.toHex('/');
        response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时
        qDebug() << "ConductivityCalibration_1 receive:" << response.toHex('/');
        result = QByteArray::fromHex("01 10 25 01 00 02 1B 04");
        if (response != result){
            emit signalInfo(Global::Cmd::ConductivityCalibration_1, Global::Result::Failure, "send ConductivityCalibration_1 2校准体系 fail");
            return;
        }

        sendData = QByteArray::fromHex("01 10 14 29 00 02 04 51 EC 40 80 2F B4");    //2点校准 CP1 5162
        qDebug() << U::getThDt()<< "ConductivityCalibration_1 2点校准 CP1 5162 send->" << sendData.toHex('/');
        response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时
        qDebug() << "ConductivityCalibration_1 receive:" << response.toHex('/');
        result = QByteArray::fromHex("01 10 14 29 00 02 95 F0");
        if (response != result){
            emit signalInfo(Global::Cmd::ConductivityCalibration_1, Global::Result::Failure, "send ConductivityCalibration_1 2点校准 CP1 5162 fail");
            return;
        }

        // sendData = QByteArray::fromHex("01 03 15 8F 00 08 71 EB");    //2点校准 5520
        // qDebug() << U::getThDt()<< "ConductivityCalibration_1 2点校准5520 send->" << sendData.toHex('/');
        // response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时
        // qDebug() << "ConductivityCalibration_1 receive:" << response.toHex('/');
        // result = QByteArray::fromHex("01 03 10 1D 58 40 80 F9 70 43 29 AB 6F 43 94 00 00 00 00 D4 0E");
        // if (response != result){
        //     emit signalInfo(Global::Result::Failure, Global::Cmd::ConductivityCalibration_1,"send ConductivityCalibration_1 2点校准5520 fail");
        //     return;
        // }
        emit signalInfo(Global::Cmd::ConductivityCalibration_1, Global::Result::Success, "");
    }
    if(cmd.m_cmd == Global::Cmd::ConductivityCalibration_2){ // 校准PH2
        QByteArray sendData = QByteArray::fromHex("01 10 14 49 00 02 04 00 00 40 E0 F8 BD");    //CP2 5194
        qDebug() << U::getThDt()<< "ConductivityCalibration_1 CP2 5194 send->" << sendData.toHex('/');
        QByteArray response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时
        qDebug() << "ConductivityCalibration_1 receive:" << response.toHex('/');
        QByteArray result = QByteArray::fromHex("01 10 14 49 00 02 95 EE");
        if (response != result){
            emit signalInfo(Global::Cmd::ConductivityCalibration_2, Global::Result::Failure, "send ConductivityCalibration_1 CP2 5194 fail");
            return;
        }

        // sendData = QByteArray::fromHex("01 03 15 97 00 08 F1 EC");    //CP2 5528
        // qDebug() << U::getThDt()<< "ConductivityCalibration_1 CP2 5528 send->" << sendData.toHex('/');
        // response = m_pSerialConductivity->sendSync(sendData,200); // 5秒超时
        // qDebug() << "ConductivityCalibration_1 receive:" << response.toHex('/');
        // result = QByteArray::fromHex("01 03 10 2A 7A 40 E0 63 18 C0 E8 98 C8 43 94 00 00 00 00 91 D0 ");
        // if (response != result){
        //     emit signalInfo(Global::Result::Failure, Global::Cmd::ConductivityCalibration_2, "send ConductivityCalibration_1 CP2 5528 fail");
        //     return;
        // }
        emit signalInfo(Global::Cmd::ConductivityCalibration_2, Global::Result::Success, "");
    }
}

bool Worker::sendData(SerialPortSync &port, char* rawData)
{
    QByteArray sendData = QByteArray(rawData, sizeof(rawData));
    qDebug() << "send_data:" << sendData.toHex();
    QByteArray response = port.sendSync(sendData,200); // 5秒超时
    qDebug() << "reve_data:" << response.toHex();
    if (response != sendData){
        return false;
    }
    return true;
}

void Worker::setRun(bool run)
{
    qDebug() << "collection"<<run;
    m_collection = run;
}

void Worker::slotUpdateParam(const QString& param, const QString& value)
{
    qDebug() <<U::getThDt()<<__FILE__ << __FUNCTION__<< param << value;
    if (param.compare("system_decimalPlaces") ==0 ){
        int decimalPlaces = value.toInt();
        this->m_weightUnitDecimalPlaces = decimalPlaces;
    }
}


void Worker::run()
{
    QString err;
    if (!init(err)){
        emit signalInfo(Global::Cmd::OpenSerial, Global::Result::Failure, err);
        return;
    }
    int i=0;
    while(true){
        // if (i>5000) LogManager::ins().log(QtDebugMsg, "\tW running>");
        if (m_collection){
            //QRandomGenerator *generator = QRandomGenerator::global();

            // 生成 [0.0, 1.0) 之间的随机浮点数，再缩放至 [0.0, 100.0)
            //float w = generator->generateDouble() * 100.0f;
            //float t = generator->generateDouble() * 100.0f;
            //float ph = generator->generateDouble() * 100.0f;
            //float c = generator->generateDouble() * 100.0f;
            //float s = generator->generateDouble() * 500.0f;
            float s     = getSpeed();
            float t     = getTemperature();
            float w     = setRound(getWeight());
            float ph    = getPh();
            float phTemp= getPhTemperature();
            float phUsageTime= getPhUsageTime();
            float c     = getConductivity();
            float cTemp = getConductivityTemperature();
            float cUsageTime= getConductivityUsageTime();
            emit signalUpdate(s, t, w, ph, phTemp, phUsageTime, c, cTemp, cUsageTime);      // 每一秒发送一个信号
            // if (i>= m_logPeriodMillionSecond){
            //     m_log.insertLog("",QString("%1").arg(s), QString("%1").arg(w),QString("%1").arg(t),QString("%1").arg(ph), QString("%1").arg(c));
            //     i=0;
            // }
        }
        int j=0;
        while(j<2000){
            Command cmd = getCmd();
            while(cmd.m_cmd != Global::Cmd::Zero){
                sendCmd(cmd);
                cmd = getCmd();
            }
            QThread::msleep(100);
            j+=100;
            i+=100;
        }
    }
}
